<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    
<meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>


<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />

<meta name="theme-color" content="#f8f5ec" />
<meta name="msapplication-navbutton-color" content="#f8f5ec">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="#f8f5ec">



  <meta name="description" content="吐血的部署"/>




  <meta name="keywords" content="flask部署, nginx, wsgi, 八一" />



  <meta name="baidu-site-verification" content="HhUstaSjr0" />



  <meta name="google-site-verification" content="UA-102975942-1" />






  <link rel="alternate" href="/atom.xml" title="八一">




  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=2.6.0" />



<link rel="canonical" href="https://bay1.top/2017/06/05/吐血的部署/"/>


<link rel="stylesheet" type="text/css" href="/css/style.css?v=2.6.0" />
<link rel="stylesheet" type="text/css" href="/css/prettify.css" media="screen" />
<link rel="stylesheet" type="text/css" href="/css/sons-of-obsidian.css" media="screen" />



  <link rel="stylesheet" type="text/css" href="/lib/fancybox/jquery.fancybox.css" />




  
  <script id="baidu_analytics">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?9a885cc9fb6cd7bcef579deb8efe8a70";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>



  <script id="google_analytics">
    (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
        (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
        m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
        })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

        ga('create', 'UA-102975942-1', 'auto');
        ga('send', 'pageview');
  </script>










    <title> 吐血的部署 - 八一 </title>
  </head>

  <body><div id="mobile-navbar" class="mobile-navbar">
  <div class="mobile-header-logo">
    <a href="/." class="logo">八一</a>
  </div>
  <div class="mobile-navbar-icon">
    <span></span>
    <span></span>
    <span></span>
  </div>
</div>

<nav id="mobile-menu" class="mobile-menu slideout-menu">
  <ul class="mobile-menu-list">
    
      <a href="/archives">
        <li class="mobile-menu-item">
          
          
            文章
          
        </li>
      </a>
    
      <a href="/tags">
        <li class="mobile-menu-item">
          
          
            标签
          
        </li>
      </a>
    
      <a href="/about">
        <li class="mobile-menu-item">
          
          
            关于/友链
          
        </li>
      </a>
    
      <a href="/search">
        <li class="mobile-menu-item">
          
          
            站内搜索
          
        </li>
      </a>
    
  </ul>
</nav>

    <div class="container" id="mobile-panel">
      <header id="header" class="header"><div class="logo-wrapper">
  <a href="/." class="logo">八一</a>
</div>

<nav class="site-navbar">
  
    <ul id="menu" class="menu">
      
        <li class="menu-item">
          <a class="menu-item-link" href="/archives">
            
            
              文章
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/tags">
            
            
              标签
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/about">
            
            
              关于/友链
            
          </a>
        </li>
      
        <li class="menu-item">
          <a class="menu-item-link" href="/search">
            
            
              站内搜索
            
          </a>
        </li>
      
    </ul>
  
</nav>

      </header>

      <main id="main" class="main">
        <div class="content-wrapper">
          <div id="content" class="content">
            
  
  <article class="post">
    <header class="post-header">
      <h1 class="post-title">
        
          吐血的部署
        
      </h1>

      <div class="post-meta">
        <span class="post-time">
          2017-06-05
        </span>
        
        
        
      </div>
    </header>

    
    
  <div class="post-toc" id="post-toc">
    <h2 class="post-toc-title">文章目录</h2>
    <div class="post-toc-content">
      <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#配置mysql"><span class="toc-text">配置mysql</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置pip和虚拟环境"><span class="toc-text">配置pip和虚拟环境</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#配置uwsgi"><span class="toc-text">配置uwsgi</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装supervisor"><span class="toc-text">安装supervisor</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#安装nginx"><span class="toc-text">安装nginx</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#后记"><span class="toc-text">后记</span></a></li></ol>
    </div>
  </div>


    <div class="post-content">
      
        <p>这个东西耗费了我一天半的时间，中间搞得快要吐血，打了几盘游戏平复心情。。。。<a id="more"></a></p>
<blockquote>
<p>本文环境<span style="color: red;">flask+mysql+python3+uwsgi+nginx+腾讯云乌班图14.04x64</span><br>用的软件<span style="color: red;">Xshell+WinSCP</span><br><a href="http://cumt.studio/" target="_blank" rel="noopener">时光匆匆</a></p>
</blockquote>
<h2 id="配置mysql"><a href="#配置mysql" class="headerlink" title="配置mysql"></a>配置mysql</h2><blockquote>
<p>首先最好添加腾讯云默认用户ubuntu到sudo列表<br>我一开始没有用这个，所以下面的有些步掫就有些显得多余</p>
</blockquote>
<figure class="highlight armasm"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">adduser </span>&lt;username&gt; sudo</span><br></pre></td></tr></table></figure>
<blockquote>
<p>安装mysql并登陆</p>
</blockquote>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install mysql-server #安装</span><br><span class="line">mysql -u youruser -pyourpassword(无空格) #登陆</span><br></pre></td></tr></table></figure>
<blockquote>
<p>建立相关的数据库和表</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">create database yourdb; <span class="comment">#建数据库</span></span><br><span class="line"></span><br><span class="line">CREATE TABLE `yourtb` (</span><br><span class="line">  `user_id` BIGINT NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `user_name` VARCHAR(<span class="number">45</span>) NULL,</span><br><span class="line">  `user_password` VARCHAR(<span class="number">300</span>) NULL,</span><br><span class="line">  PRIMARY KEY (`user_id`));</span><br><span class="line"><span class="comment">#建立用户表单</span></span><br><span class="line"></span><br><span class="line">CREATE TABLE `yourtb` (</span><br><span class="line">  `words_id` int(<span class="number">11</span>) NOT NULL AUTO_INCREMENT,</span><br><span class="line">  `words_title` varchar(<span class="number">45</span>) DEFAULT NULL,</span><br><span class="line">  `words_description` varchar(<span class="number">5000</span>) DEFAULT NULL,</span><br><span class="line">  `words_user_id` varchar(<span class="number">45</span>) DEFAULT NULL,</span><br><span class="line">  `words_date` datetime DEFAULT NULL,</span><br><span class="line">  PRIMARY KEY (`words_id`)</span><br><span class="line">) ENGINE=InnoDB AUTO_INCREMENT=<span class="number">3</span> DEFAULT character set = utf8;</span><br><span class="line"><span class="comment">#建立留言表单</span></span><br></pre></td></tr></table></figure>
<h2 id="配置pip和虚拟环境"><a href="#配置pip和虚拟环境" class="headerlink" title="配置pip和虚拟环境"></a>配置pip和虚拟环境</h2><blockquote>
<p>代码是python3写的，配置虚拟环境也要python3,很好的是有python对应pip-&gt;pip3<br>安装pip3并安装virtualenv</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install python3-pip</span><br><span class="line">pip3 install virtualenv</span><br></pre></td></tr></table></figure>
<blockquote>
<p>下面是建立网站代码的目录，你可以选择/var/www/flask/这个路径<br>也可以按照我的这个/home/www/flask</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">mkdir www </span><br><span class="line">mkdir flask</span><br><span class="line">virtualenv venv <span class="comment">#建立虚拟环境</span></span><br><span class="line">source venv/bin/activate <span class="comment">#进入虚拟环境</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>首先可以利用txt直接导入所需要的库</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">pip freeze&gt;requirements.txt <span class="comment">#导出所需库</span></span><br><span class="line">pip install -r requirements.txt <span class="comment">#导入所需库（一定要在venv下进行）</span></span><br><span class="line"><span class="comment">#实际上我要上传的只需要这两个库</span></span><br><span class="line">pip install flask</span><br><span class="line">pip install flask-mysql</span><br></pre></td></tr></table></figure>
<blockquote>
<p>上传你的应用</p>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/01/01/pSRRun.png" alt="flask"></p>
<h2 id="配置uwsgi"><a href="#配置uwsgi" class="headerlink" title="配置uwsgi"></a>配置uwsgi</h2><blockquote>
<p>命令如下</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">(venv) root@VM<span class="number">-16</span><span class="number">-119</span>-ubuntu:/home/www/flask<span class="comment"># pip install uwsgi</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>配置uwsgi:config.ini我比较喜欢用WinSCP创建文件<br>如果没有添加ubuntu到sudo,就需要将相关目录改下权限，才能顺畅的用WinSCP</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">sudo chown -R ubuntu:ubuntu /home/www/flask/</span><br><span class="line"><span class="comment">#修改到ubuntu</span></span><br><span class="line"> </span><br><span class="line"><span class="comment">#config.ini</span></span><br><span class="line">[uwsgi]</span><br><span class="line"></span><br><span class="line"><span class="comment"># uwsgi 启动时所使用的地址与端口</span></span><br><span class="line">socket = <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8080</span> </span><br><span class="line"></span><br><span class="line"><span class="comment"># 指向网站目录</span></span><br><span class="line">chdir = /home/www/flask </span><br><span class="line"></span><br><span class="line"><span class="comment"># python 启动程序文件</span></span><br><span class="line">wsgi-file = app.py </span><br><span class="line"></span><br><span class="line"><span class="comment"># python 程序内用以启动的 application 变量名</span></span><br><span class="line">callable = app </span><br><span class="line"></span><br><span class="line"><span class="comment"># 处理器数</span></span><br><span class="line">processes = <span class="number">4</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 线程数</span></span><br><span class="line">threads = <span class="number">2</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#状态检测地址</span></span><br><span class="line">stats = <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">9191</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以利用下面的命令运行一下uwsgi<br>成功如图</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">uwsgi config.ini</span><br></pre></td></tr></table></figure>
<p><img src="https://s1.ax1x.com/2018/01/01/pShS6H.png" alt="uwsgi"></p>
<h2 id="安装supervisor"><a href="#安装supervisor" class="headerlink" title="安装supervisor"></a>安装supervisor</h2><p><a href="http://supervisord.org/configuration.html" target="_blank" rel="noopener">Supervisor</a></p>
<blockquote>
<p>supervisor可以同时启动多个应用，当某个应用挂掉的时候，他可以自动重启该应用，保证可用性<br>它的默认配置文件不需要修改，添加一个用于启动你的项目的uwsgi的supervisor配置 flask_supervisor.conf</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install supervisor</span><br><span class="line"></span><br><span class="line">/etc/supervisor/conf.d/ <span class="comment">#在这里新建flask_supervisor.conf</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#flask_supervisor.conf</span></span><br><span class="line"></span><br><span class="line">[program:flask]</span><br><span class="line"><span class="comment"># 启动命令入口</span></span><br><span class="line">command=/home/www/flask/venv/bin/uwsgi /home/www/flask/config.ini</span><br><span class="line"></span><br><span class="line"><span class="comment"># 命令程序所在目录</span></span><br><span class="line">directory=/home/www/flask</span><br><span class="line"><span class="comment">#运行命令的用户名</span></span><br><span class="line">user=root</span><br><span class="line">        </span><br><span class="line">autostart=true</span><br><span class="line">autorestart=true</span><br><span class="line"><span class="comment">#日志地址</span></span><br><span class="line">stdout_logfile=/home/www/flask/logs/uwsgi_supervisor.log</span><br></pre></td></tr></table></figure>
<blockquote>
<p>需要创建日志文件uwsgi_supervisor.log<br>supervisor的启动和停止</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo service supervisor start</span><br><span class="line">sudo service supervisor stop</span><br></pre></td></tr></table></figure>
<h2 id="安装nginx"><a href="#安装nginx" class="headerlink" title="安装nginx"></a>安装nginx</h2><blockquote>
<p>安装nginx并且配置default文件</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-get install nginx</span><br><span class="line"></span><br><span class="line">/etc/nginx/sites-available <span class="comment">#default路径</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#default内容</span></span><br><span class="line"></span><br><span class="line">server &#123;</span><br><span class="line">      listen  <span class="number">80</span>;</span><br><span class="line">      server_name XXX.XXX.XXX; </span><br><span class="line">    </span><br><span class="line">       location /static &#123;</span><br><span class="line">    	root /home/www/flask/;</span><br><span class="line">       &#125;</span><br><span class="line">      location / &#123;</span><br><span class="line">        include      uwsgi_params;</span><br><span class="line">        uwsgi_pass   <span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8080</span>;  </span><br><span class="line">        uwsgi_param UWSGI_PYHOME /home/www/flask/venv; <span class="comment"># 指向虚拟环境目录</span></span><br><span class="line">        uwsgi_param UWSGI_CHDIR  /home/www/flask; <span class="comment"># 指向网站根目录</span></span><br><span class="line">        uwsgi_param UWSGI_SCRIPT app:app; <span class="comment"># 指定启动程序</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<blockquote>
<p>每次改完配置需要重启nginx</p>
</blockquote>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo service nginx restart</span><br></pre></td></tr></table></figure>
<h2 id="后记"><a href="#后记" class="headerlink" title="后记"></a>后记</h2><blockquote>
<p>部署成功之后，一直登陆显示密码错误，这问题头疼了好几天，最后还是老组长耐心帮我修改好了，跪谢！！！<br>原因竟然是建字段设置不准确</p>
</blockquote>
<p><img src="https://s1.ax1x.com/2018/01/01/pSRNjA.png" alt="error-1"><br><img src="https://s1.ax1x.com/2018/01/01/pSRanI.png" alt="error-2"></p>
<p><strong>参考链接：</strong><br> <a href="http://www.cnblogs.com/Ray-liang/p/4173923.html?utm_source=tuicool&amp;utm_medium=referral" target="_blank" rel="noopener">阿里云部署 Flask + WSGI + Nginx 详解</a><br> <a href="http://www.oschina.net/translate/serving-flask-with-nginx-on-ubuntu" target="_blank" rel="noopener">在 Ubuntu 上使用 Nginx 部署 Flask 应用</a><br> <a href="http://docs.pythontab.com/python/python3.4/venv.html" target="_blank" rel="noopener">python3中文手册</a></p>

      
    </div>

    
      
      



      
      
    

    
      <footer class="post-footer">
        
          <div class="post-tags">
            
              <a href="/tags/flask部署/">flask部署</a>
            
              <a href="/tags/nginx/">nginx</a>
            
              <a href="/tags/wsgi/">wsgi</a>
            
          </div>
        
        
        
  <nav class="post-nav">
    
      <a class="prev" href="/2017/06/07/CUMT2017赛宁杯/">
        <i class="iconfont icon-left"></i>
        <span class="prev-text nav-default">CUMT2017赛宁杯</span>
        <span class="prev-text nav-mobile">上一篇</span>
      </a>
    
    
      <a class="next" href="/2017/06/02/六一任务-留言板/">
        <span class="next-text nav-default">六一任务---留言板</span>
        <span class="prev-text nav-mobile">下一篇</span>
        <i class="iconfont icon-right"></i>
      </a>
    
  </nav>

      </footer>
    

  </article>


          </div>
          
  <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="//disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div> 
    </div>
  </div>


        </div>
      </main>

      <footer id="footer" class="footer">

  <div class="social-links">
    
      
        
          <a href="https://github.com/bay1" class="iconfont icon-github" title="github"></a>
        
      
    
      
        
          <a href="http://weibo.com/3190704711/profile?topnav=1&wvr=6&is_all=1" class="iconfont icon-weibo" title="weibo"></a>
        
      
    
      
    
      
    
      
    
    
    
  </div>


<div class="copyright">
  <span class="copyright-year">
    
    &copy; 
     
      2016 - 
    
    2018
    <span class="author">bay1</span>
  </span>
</div>
      </footer>

      <div class="back-to-top" id="back-to-top">
        <i class="iconfont icon-up"></i>
      </div>
    </div>

    
  
  <script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://bay1.top/2017/06/05/吐血的部署/';
        this.page.identifier = '2017/06/05/吐血的部署/';
        this.page.title = '吐血的部署';
    };
    (function() {
    var d = document, s = d.createElement('script');

    s.src = '//https-blog-flywinky-top-1.disqus.com/embed.js';

    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();  
  </script>



    
  





  
    <script type="text/javascript" src="/lib/jquery/jquery-3.1.1.min.js"></script>
  

  
    <script type="text/javascript" src="/lib/slideout/slideout.js"></script>
  

  
    <script type="text/javascript" src="/lib/fancybox/jquery.fancybox.pack.js"></script>
  


    <script type="text/javascript" src="/js/src/even.js?v=2.6.0"></script>
<script type="text/javascript" src="/js/src/bootstrap.js?v=2.6.0"></script>
<script src="/js/prettify.js"></script>
<script type="text/javascript">
$(document).ready(function(){
 $('pre').addClass('prettyprint');
   prettyPrint();
 })
</script>
  </body>
</html>
